package edu.hnu.tradeserver.service.impl;

import static edu.hnu.tradeserver.common.util.TimeUtil.calculateStartDate;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import edu.hnu.commonutils.Result;
import edu.hnu.tradeserver.entity.Trades;
import edu.hnu.tradeserver.entity.Trades;
import edu.hnu.tradeserver.mapper.TradesMapper;
import edu.hnu.tradeserver.service.TradesService;
import edu.hnu.tradeserver.mapper.TradesMapper;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
* @author 64132
* @description 针对表【trades】的数据库操作Service实现
* @createDate 2023-08-11 01:05:06
*/
@Service
public class TradesServiceImpl extends ServiceImpl<TradesMapper, Trades>
    implements TradesService{

    @Autowired
    private TradesMapper tradesMapper;

    @Override
    public Result tradeInfo() {
        QueryWrapper<Trades> queryWrapper = new QueryWrapper<>();
        List<Trades> TradesList = tradesMapper.selectList(queryWrapper);
        return Result.ok().data(TradesList);
    }

    @Override
    public Result createTrade(Trades trades) {
        tradesMapper.insert(trades);
        return Result.ok();
    }

    @Override
    public List<Trades> getTradesInfo(String timeRange) {
        QueryWrapper<Trades> queryWrapper = new QueryWrapper<>();

        // 根据时间范围设置查询条件
        LocalDate endDate = LocalDate.now();  // 结束时间为当前时间
        LocalDate startDate = calculateStartDate(timeRange, endDate);  // 计算起始时间

        queryWrapper.between("transaction_date", startDate, endDate);

        List<Trades> tradesList = tradesMapper.selectList(queryWrapper);
        return tradesList;
    }


}




